package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.CustomersPresenter;
import com.emser.unc.client.resources.GlobalResources;
import com.emser.unc.serializable.CustomerDTO;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.Range;

public class CustomersView extends Composite implements CustomersPresenter.Display {
	private VerticalPanel verticalPanel;
	private CellTable<CustomerDTO> customersTable;
	private MultiSelectionModel<CustomerDTO> selectionModel;
	private Button btnAdd, btnRemove;
	private PushButton btnSearch;
	private Column<CustomerDTO, String> editColumn;
	private SimplePager pager;
	private final GlobalResources resource;
	private TextBox searchParam;
	private MessageWView messageDelete;
	private int pageS = CloudMobile.constants.pageSizeCP();
	private ProvidesKey<CustomerDTO> KEY_PROVIDER = new ProvidesKey<CustomerDTO>() {
	      @Override
	      public Object getKey(CustomerDTO item) {
	        return item == null ? null : item.getCode();
	      }
	    };
	private BusyIndicatorView busyIndicator = new BusyIndicatorView(CloudMobile.constants.msjWorking());;
	private int heightSPanel;
	public CustomersView() {	
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		heightSPanel = CloudMobile.get().getHeightPanelCenter() - 100;
		initHPanelTitle();
		initScrollPanel();
	    initPager();
	    resource = GWT.create(GlobalResources.class);
	    messageDelete = new MessageWView();
	    messageDelete.getLblMessageW().setText(CloudMobile.constants.msjWarningDelete());
	    messageDelete.hide();
	    busyIndicator.hide();
	}
	private void initScrollPanel() {
		ScrollPanel sPanel = new ScrollPanel();
		sPanel.setStyleName("gwt-sPanelCenter");
		sPanel.setSize("100%-1px", heightSPanel + "px");
		selectionModel = new MultiSelectionModel<CustomerDTO>(KEY_PROVIDER);
		customersTable= new CellTable<CustomerDTO>(pageS, KEY_PROVIDER);
	    initColumnsTable();
	    customersTable.setSelectionModel(selectionModel, DefaultSelectionEventManager.<CustomerDTO>createCheckboxManager());
	    customersTable.setSize("100%", "100%");
	    sPanel.setWidget(customersTable);
	    verticalPanel.add(sPanel);
	    verticalPanel.setCellHorizontalAlignment(sPanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(sPanel, "100%");
	    verticalPanel.setCellHeight(sPanel, heightSPanel+"px");
		
	}
	private void initPager() {
		SimplePanel simplePanel = new SimplePanel();
		simplePanel.setStyleName("gwt-SimplePanelCenter1");
	    verticalPanel.add(simplePanel);
	    verticalPanel.setCellVerticalAlignment(simplePanel, HasVerticalAlignment.ALIGN_MIDDLE);
	    simplePanel.setSize("400px", "40px");
	    verticalPanel.setCellHorizontalAlignment(simplePanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(simplePanel, "100%");
	    verticalPanel.setCellHeight(simplePanel, "40px");
	    pager = new SimplePager(TextLocation.CENTER){
	    	@Override
	    	public void setPageStart(int index) {
	    	  if (getDisplay() != null) {
	    	    Range range = getDisplay().getVisibleRange();
	    	    int pageSize = range.getLength();
	    	    index = Math.max(0, index);
	    	    if (index != range.getStart()) {
	    	      getDisplay().setVisibleRange(index, pageSize);
	    	    }
	    	  }
	    	}
	    };
	    pager.setStyleName("gwt-pager1");
	    pager.setRangeLimited(true);
	    pager.setDisplay(customersTable);
	    pager.setSize("400px", "15px");
	    pager.setPageSize(pageS);
	    pager.setPage(0);
	   simplePanel.add(pager);
	}   
	
	private void initColumnsTable() {
		Column<CustomerDTO, Boolean> column = new Column<CustomerDTO, Boolean>(new CheckboxCell()) {
			@Override
			public Boolean getValue(CustomerDTO object) {
				return selectionModel.isSelected(object);
			}
		};
		column.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(column);
		Column<CustomerDTO , ImageResource> imageColumn =
			  new Column<CustomerDTO , ImageResource>(new ImageResourceCell()) {
			    @Override
			    public ImageResource getValue(CustomerDTO  object) {
			      return resource.getImageCustomer();
			    }
			  };
		imageColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(imageColumn, "");
		TextColumn<CustomerDTO> idColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getCode().toString();
			}
		};
		idColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(idColumn, CloudMobile.constants.number());
		TextColumn<CustomerDTO> firstNameColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getFirstName();
			}
		};
		firstNameColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(firstNameColumn, CloudMobile.constants.name());	
		TextColumn<CustomerDTO> lastNameColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getLastName();
			}
		};
		lastNameColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(lastNameColumn, CloudMobile.constants.lastName());
		TextColumn<CustomerDTO> addressColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getAddress();
			}
		};
		addressColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(addressColumn, CloudMobile.constants.address());
		TextColumn<CustomerDTO> cityColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getCity();
			}
		};
		cityColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(cityColumn, CloudMobile.constants.city());
		TextColumn<CustomerDTO> cpColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getZipCode();
			}
		};
		cpColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(cpColumn, CloudMobile.constants.cp());
		TextColumn<CustomerDTO> telephoneColumn = new TextColumn<CustomerDTO>() {
			@Override
			public String getValue(CustomerDTO object) {
				return object.getTelephone();
			}
		};
		telephoneColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(telephoneColumn, CloudMobile.constants.telephone());	
		editColumn = new Column<CustomerDTO, String>(new ButtonCell()) {
			@Override
			public String getValue(CustomerDTO object) {
				return " Editar ";
			}
		};
		editColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		customersTable.addColumn(editColumn, "");	
	}
	
	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-hPanelCenter");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setSize("100%", "40px");
	    verticalPanel.add(horizontalPanel);  
	    verticalPanel.setCellHeight(horizontalPanel, "40px");
	    verticalPanel.setCellWidth(horizontalPanel, "100%");
	    
	    Grid grid = new Grid(1, 2);
	    horizontalPanel.add(grid);
	    horizontalPanel.setCellVerticalAlignment(grid, HasVerticalAlignment.ALIGN_MIDDLE);
	    btnAdd = new Button(CloudMobile.constants.add());
	    grid.setWidget(0, 0, btnAdd);
	    btnAdd.setSize("112", "28");
	        
	    btnRemove = new Button(CloudMobile.constants.remove());
	    grid.setWidget(0, 1, btnRemove);
	    btnRemove.setSize("112", "28");
	    grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    
	    Grid grid_1 = new Grid(1, 2);
	    horizontalPanel.add(grid_1);
	    horizontalPanel.setCellVerticalAlignment(grid_1, HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setCellHorizontalAlignment(grid_1, HasHorizontalAlignment.ALIGN_RIGHT);
	    searchParam = new TextBox();
	    grid_1.setWidget(0, 0, searchParam);
	    searchParam.setSize("200px", "28");
	    
	    btnSearch = new PushButton(CloudMobile.constants.search());
	    grid_1.setWidget(0, 1, btnSearch);
	    grid_1.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
	    grid_1.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	}
	
	@Override
	public HasClickHandlers getAddButton() {
		return btnAdd;
	}
	
	@Override
	public HasClickHandlers getRemoveButton() {
		return btnRemove;
	}
	
	@Override
	public MultiSelectionModel<CustomerDTO> getSelectionModel() {
		return selectionModel;
	}
	@Override
	public HasData<CustomerDTO> getTable() {
		return customersTable;
	}
	@Override
	public Column<CustomerDTO, String> getEditColumn() {
		return editColumn;
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}
	@Override
	public void enableDelete(boolean flag) {
		btnRemove.setEnabled(flag);
	}
	@Override
	public HasClickHandlers getSearchButton() {
		return btnSearch;
	}
	@Override
	public HasValue<String> getSearchParam() {
		return searchParam;
	}
	@Override
	public void showWarning() {
		messageDelete.show();		
	}
	@Override
	public void disableWarning() {
		messageDelete.hide();	
	}
	@Override
	public HasClickHandlers getBtnPopUpDCancel() {
		return messageDelete.getBtnCancel();
	}
	@Override
	public HasClickHandlers getBtnPopUpDDelete() {
		return messageDelete.getBtnDelete();
	}
	@Override
	public void enableBtnAdd(boolean enabled) {
		btnAdd.setEnabled(enabled);	
	}
	@Override
	public void enableBtnPopUpDelete(boolean enabled) {
		messageDelete.getBtnDelete().setEnabled(enabled);	
	}
	@Override
	public void enableBtnSearch(boolean enabled) {
		btnSearch.setEnabled(enabled);
	}
	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}
	@Override
	public int getPage() {
		return pager.getPage();
	}
	@Override
	public void setPage(int page) {
		pager.setPage(page);
		
	}
}
